package mysocket;

import java.io.*;
import java.net.Socket;
import java.util.Iterator;
import java.util.Map;

/**
 * @author 小也
 * @create 2021/6/15 17:35
 */
public class STC implements Runnable{

    public static final String BYEBYE = "byebye";

    private Socket socket;
    private Map<String,Socket> clientMap;

    public STC(Socket socket, Map<String, Socket> clientMap) {
        this.socket = socket;
        this.clientMap = clientMap;
    }

    @Override
    public void run() {
        InputStream inputStream = null;
        DataInputStream dataInputStream = null;

        OutputStream outputStream = null;
        DataOutputStream dataOutputStream = null;
        try{
            try {
                inputStream = socket.getInputStream();
                dataInputStream = new DataInputStream(inputStream);

                outputStream = socket.getOutputStream();
                dataOutputStream = new DataOutputStream(outputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }

            while (true){
                String temp = null;
                try {
                    temp = dataInputStream.readUTF();
                    if (temp != null){
                        String[] str = temp.split(" ");
                        String from = str[0];
                        String to = str[1];
                        String word = str[2];
//                        if (to.equals("*")){
//                            Iterator<Map.Entry<String, Socket>> iterator = clientMap.entrySet().iterator();
//                            while (iterator.hasNext()){
//                                Socket toSocket = (Socket) iterator.next();
//
//                                OutputStream toOutputStream = toSocket.getOutputStream();
//                                DataOutputStream toDataOutputStream = new DataOutputStream(toOutputStream);
//
//                                toDataOutputStream.writeUTF( from + "：" + word);
//                            }
////                            if (word.equals(BYEBYE)){
////                                if (dataInputStream != null){
////                                    try {
////                                        dataInputStream.close();
////                                    } catch (IOException e) {
////                                        e.printStackTrace();
////                                    }
////                                }
////                                if (inputStream != null){
////                                    try {
////                                        inputStream.close();
////                                    } catch (IOException e) {
////                                        e.printStackTrace();
////                                    }
////                                }
////                                if (dataOutputStream != null){
////                                    try {
////                                        dataOutputStream.close();
////                                    } catch (IOException e) {
////                                        e.printStackTrace();
////                                    }
////                                }
////                                if (outputStream != null){
////                                    try {
////                                        outputStream.close();
////                                    } catch (IOException e) {
////                                        e.printStackTrace();
////                                    }
////                                }
////                                if (socket != null){
////                                    try {
////                                        socket.close();
////                                    } catch (IOException e) {
////                                        e.printStackTrace();
////                                    }
////                                }
////                            }
//                        }
                        if (clientMap.get(to) == null){
                            dataOutputStream.writeUTF("服务器：" + to + " 未在线");
                        }else {
                            Socket toSocket = clientMap.get(to);

                            OutputStream toOutputStream = toSocket.getOutputStream();
                            DataOutputStream toDataOutputStream = new DataOutputStream(toOutputStream);

                            toDataOutputStream.writeUTF( from + "：" + word);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            if (dataInputStream != null){
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dataOutputStream != null){
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
